using System.Collections.Generic;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TFS.Kanban.Domain;
using TFS.Kanban.Domain.Services;

namespace TFS.Kanban.VS2010.Services
{
    public class ProjectService : IProjectService
    {
        private readonly IProjectCollectionService _projectCollectionService;
        private ProjectCollection _projects;

        public ProjectService(IProjectCollectionService projectCollectionService)
        {
            _projectCollectionService = projectCollectionService;
        }

        public IEnumerable<IProject> GetProjects()
        {
            var result = new HashSet<IProject>();
            var projectCollection = _projectCollectionService.GetProjectCollection();
            var workItemStore = new WorkItemStore(projectCollection);

            _projects = workItemStore.Projects;
            foreach (Microsoft.TeamFoundation.WorkItemTracking.Client.Project project in _projects)
            {
                result.Add(new Domain.Project { Id = project.Id, Name = project.Name});
            }

            return result;
        }

        public Microsoft.TeamFoundation.WorkItemTracking.Client.Project GetTfsProject(int id)
        {
            return _projects.GetById(id);
        }
    }
}